package com.ymatou.iapi.productquery.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productquery.parameter.TopProduct.TopProductInLiveBean;
import com.ymatou.iapi.productquery.service.GetTopProductListByLiveIdCall;
import com.ymatou.iapi.sellerproduct.service.CancelRecommendCall;
import com.ymatou.iapi.sellerproduct.service.SetRecommendCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetTopProductListByLiveId {
	private static GetTopProductListByLiveIdCall getTopProductListByLiveIdCall = new GetTopProductListByLiveIdCall();
	
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static String sellerName = EnvSetup.getData("productpriceSellerName");	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询直播中置顶商品列表");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getTopProductListByLiveIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTopProductListByLiveId_001() {
		Logger.start(true,"正在进行直播 置顶，移出置顶，结束直播等查询");
		try{
			//发布商品加入直播
			JSONObject liveBean = LiveCallService.createLiveInProgress(sellerName,sellerId);
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			String productId1 =	SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null).getJSONObject("Data").getString("ProductID");
			String productId2 =	SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null).getJSONObject("Data").getString("ProductID");
			String productId3 =	SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null).getJSONObject("Data").getString("ProductID");
			
			SellerProductCallService.addProductsInLive(sellerId,liveId,Arrays.asList(productId1,productId2,productId3));

			//没有置顶商品
			checkTopProdByLiveId(liveId,new ArrayList<String>());
			
			//一个置顶商品 productId1
			SetRecommendCall.setrecommendcall(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId, "ProductId", productId1));		
			YMTDateUtil.waitTime(10);
			checkTopProdByLiveId(liveId,Arrays.asList(productId1));
			
			//三个置顶商品 productId1,productId2,productId3
			SetRecommendCall.setrecommendcall(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId, "ProductId", productId2));		
			SetRecommendCall.setrecommendcall(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId, "ProductId", productId3));		
			YMTDateUtil.waitTime(10);
			checkTopProdByLiveId(liveId,Arrays.asList(productId1,productId2,productId3));
			
			
			//一个商品移出置顶 productId1
			 CancelRecommendCall.cancelrecommendcall(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId, "ProductId", productId1));
			 YMTDateUtil.waitTime(10);
			 checkTopProdByLiveId(liveId,Arrays.asList(productId2,productId3));
			 
			 //一个暂停销售 productId2
			 SellerProductCallService.suspendSaleProductInLive(sellerId, liveId, productId2);
			 YMTDateUtil.waitTime(10);
			 checkTopProdByLiveId(liveId,Arrays.asList(productId3));
			 
			 //一个开始销售 productId2
			 SellerProductCallService.startSaleProductInLive(sellerId, liveId, productId2);
			 YMTDateUtil.waitTime(10);
			 checkTopProdByLiveId(liveId,Arrays.asList(productId2,productId3));
			 
			 
			 //结束直播
			 LiveCallService.closeLive(liveId, sellerId);
			 YMTDateUtil.waitTime(10);
			 checkTopProdByLiveId(liveId,new ArrayList<String>());
			 
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTopProductListByLiveId_002() {
		Logger.start(true,"未开始直播查询");
		try{
			JSONObject liveBean = LiveCallService.createLiveBeginInFuture(sellerName,sellerId);
			int liveId = liveBean.getJSONObject("Data").getInt("ActivityId");
			
			String productId1 =	SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null).getJSONObject("Data").getString("ProductID");
			String productId2 =	SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null).getJSONObject("Data").getString("ProductID");
			String productId3 =	SellerProductCallService.addNewProductNotOnSale(sellerId,sellerName,null).getJSONObject("Data").getString("ProductID");
			
			SellerProductCallService.addProductsInLive(sellerId,liveId,Arrays.asList(productId1,productId2,productId3));

			//没有置顶商品
			checkTopProdByLiveId(liveId,new ArrayList<String>());
			
			//一个置顶商品 productId1
			SetRecommendCall.setrecommendcall(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId, "ProductId", productId1));		
			checkTopProdByLiveId(liveId,new ArrayList<String>());
			
			//三个置顶商品 productId1,productId2,productId3
			SetRecommendCall.setrecommendcall(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId, "ProductId", productId2));		
			SetRecommendCall.setrecommendcall(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId, "ProductId", productId3));		
			checkTopProdByLiveId(liveId,new ArrayList<String>());
			
		}catch(Exception e){
			
		}
	}
	public static void checkTopProdByLiveId(int liveId,List<String> prodList) throws Exception{
		JSONObject ret = getTopProductListByLiveIdCall.search(Arrays.asList(((Integer)liveId).toString()));
		String chkdesc = "GetTopProductListByLiveId";
		
		if (prodList.size()>0){
			List<TopProductInLiveBean> tgtTopProductInLiveBeanList = ProductQueryCallService.geTopProductInLiveBean(liveId);
			//返回值取数据
			List<TopProductInLiveBean> actTopProductInLiveBeanList = new ArrayList<>();
			JSONArray prodListAry = ret.getJSONObject("Data").getJSONArray("ProductList");
			for(int i=0;i<prodListAry.length();i++){
				actTopProductInLiveBeanList.add(ProductQueryCallService.getBeanFromJson(prodListAry.getJSONObject(i),TopProductInLiveBean.class));
			}
			
			ProductQueryCallService.compareTopProductInLiveBean(prodList,tgtTopProductInLiveBeanList,actTopProductInLiveBeanList,chkdesc);
	
		}else{
			Logger.verifyEquals(true,ret.getJSONObject("Data").isNull("ProductList"),chkdesc + ", 应该没有置顶商品 live:"+ liveId);
			
		}
	}
	
	
	
	
	
	
	
	
	
	
	
}
